package cn.com.gridinfo.sooc.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;

import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * 获取渠道号
 */
public class GetChannel {
	private Activity context;

	public GetChannel(Activity context) {
		super();
		this.context = context;
	}

	public static String getChannel(Context context) {
		ApplicationInfo appinfo = context.getApplicationInfo();
		String sourceDir = appinfo.sourceDir;
		String ret = "";
		ZipFile zipfile = null;
		try {
			zipfile = new ZipFile(sourceDir);
			Enumeration<?> entries = zipfile.entries();
			while (entries.hasMoreElements()) {
				ZipEntry entry = ((ZipEntry) entries.nextElement());
				String entryName = entry.getName();
				if (entryName.startsWith("META-INF/mtchannel")) {
					ret = entryName;
					break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (zipfile != null) {
				try {
					zipfile.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		String[] split = ret.split("_");
		if (split != null && split.length >= 2) {
			return ret.substring(split[0].length() + 1);

		} else {
			return "";
		}
	}

	/**
	 * 获取渠道号
	 * @param context
	 * @return
     */
	public static String getChannelNo(Context context){
		return GetChannel.getChannel(context).replaceAll("replace_", "").toLowerCase();
	}
}
